package com.file.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.ChildMeter;
import com.entity.Customer;
import com.entity.Meter;
import com.entity.Org;
import com.entity.Sysdic;
import com.file.entity.FileCustomer;
import com.file.entity.FileMeter;
import com.interfaces.ChildMeterDao;
import com.interfaces.CustomerDao;
import com.interfaces.CustomerMeterDao;
import com.interfaces.MeterDao;
import com.interfaces.OrgDao;
import com.interfaces.SysdicDao;
import com.mysql.fabric.xmlrpc.base.Array;
import com.setting.service.SysdicService;
import com.utils.JsonResult;
import com.utils.Utils;

/**
 * @Title: CustomerService.java
 * @Package com.service
 * @author 孙梦楠
 * @date 2017年7月24日 上午8:45:02
 * @version V1.0.0
 */

@Service
public class CustomerService {

	@Autowired
	private SysdicDao sysdicdao;

	@Autowired
	private CustomerDao customerdao;

	@Autowired
	private SysdicService sysdicservice;

	@Autowired
	private CustomerMeterDao customermeterdao;

	@Autowired
	private MeterDao meterdao;

	@Autowired
	private OrgDao orgdao;

	@Autowired
	private MeterService meterservice;

	@Autowired
	private ChildMeterDao childmeterdao;

	private static final Logger log = LoggerFactory.getLogger(CustomerService.class);

	// // 列表
	// @Transactional
	// public JsonResult<List<FileCustomer>> listcustomer(String opeorgcookie,
	// String languagecookie) {
	//
	// List<Customer> customers =
	// customerdao.findByCustomerorg(Integer.valueOf(opeorgcookie));
	// List<FileCustomer> filecustomers = new ArrayList<FileCustomer>();
	//
	// if (customers.size() != 0) {
	// for (int i = 0; i < customers.size(); i++) {
	//
	// Customer customer = customers.get(i);
	//
	// filecustomers.add(dofilecustomer(customer, languagecookie));
	// }
	// }
	//
	// return new JsonResult<List<FileCustomer>>(93, "展示用户所有列表", filecustomers);
	// }

	// 状态列表
	@Transactional
	public JsonResult<List<FileCustomer>> listallcustomer(String opeorgcookie, String customerstatus, Integer State,
			String Massage, String languagecookie) {

		List<Customer> customers = customerdao.findByCustomerorgAndCustomerstatus(Integer.valueOf(opeorgcookie),
				customerstatus);
		List<FileCustomer> filecustomers = new ArrayList<FileCustomer>();

		if (customers.size() != 0) {
			for (int i = 0; i < customers.size(); i++) {
				Customer customer = customers.get(i);
				filecustomers.add(dofilecustomer(customer, languagecookie));
			}
		}

		return new JsonResult<List<FileCustomer>>(State, Massage, filecustomers);
	}

	@Transactional
	public JsonResult<List<FileCustomer>> listallcustomer(String opeorgcookie, String opetypecookie,
			String customerstatus, String customerrechargestatus, Integer State, String Massage, String customeraccount,
			String customername, String customeraddress, String languagecookie, String childtotalstatus) {

		List<Customer> customers = new ArrayList<Customer>();

		if (Utils.isNull(customeraccount) && Utils.isNull(customername) && Utils.isNull(customeraddress)) {

			customers = customerdao.findByCustomerorgAndCustomertypeAndCustomerstatusAndCustomerrechargestatus(
					Integer.valueOf(opeorgcookie), opetypecookie, customerstatus, customerrechargestatus);
		} else {

			customers = customerdao.findByConditions_recharge(customeraccount, customername, customeraddress,
					Integer.valueOf(opeorgcookie), opetypecookie, customerstatus, customerrechargestatus);
		}

		List<FileCustomer> filecustomers = new ArrayList<FileCustomer>();

		if (customers.size() != 0) {
			if (childtotalstatus.equals("总")) {
				for (int i = 0; i < customers.size(); i++) {
					Customer customer = customers.get(i);
					// 和用户关联的表ID
					Integer meterid = customermeterdao.findByCustomerid2(customer.getCustomerid()).getPk().getMeterid();
					// 总分关系
					ChildMeter cm = childmeterdao.findByMeterid(meterid);
					// 如果已设置表而且是总表
					if (cm != null && cm.getFathermeterid() == 0) {
						filecustomers.add(dofilecustomer(customer, languagecookie));
					}
				}
			} else {// 分表
				for (int i = 0; i < customers.size(); i++) {
					Customer customer = customers.get(i);
					// 和用户关联的表ID
					Integer meterid = customermeterdao.findByCustomerid2(customer.getCustomerid()).getPk().getMeterid();
					// 总分关系
					ChildMeter cm = childmeterdao.findByMeterid(meterid);
					// 如果是未设置表或者是分表
					if (cm == null// 未设置表
							|| (cm != null && cm.getFathermeterid() == null)// 设置分表但是未关联总表
							|| (cm != null && cm.getFathermeterid() !=0)// 关联过的分表
					) {
						filecustomers.add(dofilecustomer(customer, languagecookie));
					}
				}
			}

		}

		return new JsonResult<List<FileCustomer>>(State, Massage, filecustomers);
	}

	// 条件展示
	@Transactional
	public JsonResult<List<FileCustomer>> listCustomerByConditions(String customeraccount, String customername,
			String customeraddress, String customertype, String opeorgcookie, String customerstatus,
			String customerphone, String customerwechat, String languagecookie) {

		if (Utils.isNull(customeraccount) && Utils.isNull(customername) && Utils.isNull(customeraddress)
				&& Utils.isNull(customerstatus) && Utils.isNull(customerphone) && Utils.isNull(customerwechat)) {
			// 如果全是NULL，返回所有

			List<Customer> customers = new ArrayList<Customer>();
			List<FileCustomer> filecustomers = new ArrayList<FileCustomer>();
			if (opeorgcookie.equals("1")) {
				customers = customerdao.findByCustomertypeAndCustomerrechargestatusNot(customertype, "4");
				for (Customer customer : customers) {
					FileCustomer fileCustomer = dofilecustomer(customer, languagecookie);
					filecustomers.add(fileCustomer);
				}

			} else if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {// 公司层级
				List<Org> orgs = orgdao.findByOrgfather(opeorgcookie);
				for (Org org : orgs) {
					customers = customerdao.findByCustomerorgAndCustomertypeAndCustomerrechargestatusNot(
							Integer.parseInt(org.getOrgvalue()), customertype, "4");
					for (Customer customer : customers) {
						FileCustomer fileCustomer = dofilecustomer(customer, languagecookie);
						filecustomers.add(fileCustomer);
					}
				}
			} else {// 小区层级
				customers = customerdao.findByCustomerorgAndCustomertypeAndCustomerrechargestatusNot(
						Integer.parseInt(opeorgcookie), customertype, "4");
				for (Customer customer : customers) {
					FileCustomer fileCustomer = dofilecustomer(customer, languagecookie);
					filecustomers.add(fileCustomer);
				}
			}
			return new JsonResult<List<FileCustomer>>(93, "展示用户所有列表", filecustomers);

		} else {

			List<Customer> customers = new ArrayList<Customer>();
			List<FileCustomer> filecustomers = new ArrayList<FileCustomer>();
			if (opeorgcookie.equals("1")) {
				customers = customerdao.findByConditions(customeraccount, customername, customeraddress, customertype,
						null, customerstatus, customerphone, customerwechat);
				for (Customer customer : customers) {
					FileCustomer fileCustomer = dofilecustomer(customer, languagecookie);
					filecustomers.add(fileCustomer);
				}

			} else if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {// 公司层级
				List<Org> orgs = orgdao.findByOrgfather(opeorgcookie);
				for (Org org : orgs) {
					customers = customerdao.findByConditions(customeraccount, customername, customeraddress,
							customertype, Integer.parseInt(org.getOrgvalue()), customerstatus, customerphone,
							customerwechat);
					for (Customer customer : customers) {
						FileCustomer fileCustomer = dofilecustomer(customer, languagecookie);
						filecustomers.add(fileCustomer);
					}
				}
			} else {// 小区层级
				customers = customerdao.findByConditions(customeraccount, customername, customeraddress, customertype,
						Integer.valueOf(opeorgcookie), customerstatus, customerphone, customerwechat);
				for (Customer customer : customers) {
					FileCustomer fileCustomer = dofilecustomer(customer, languagecookie);
					filecustomers.add(fileCustomer);
				}
			}

			log.info("ListCustomerByConditionsSuccess");
			return new JsonResult<List<FileCustomer>>(98, "展示用户条件查询列表", filecustomers);
		}
	}

	// 增加用户
	@Transactional
	public JsonResult<Customer> addcustomer(String customername, String customeraddress, String customertype,
			String opeorgcookie, String customerphone, String customerwechat) {

		if (Utils.isNullOrEmpty(customername) || Utils.isNullOrEmpty(customeraddress)
				|| Utils.isNullOrEmpty(customertype) || Utils.isNullOrEmpty(opeorgcookie)
				|| Utils.isNullOrEmpty(customerphone)) {

			return new JsonResult<Customer>(92, "增加用户失败..数值不能为空", null);
		} else {

			Customer customer = new Customer();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String customeraccount = sdf.format(new Date());
			if (customerdao.findByCustomeraccountLikeDESC(customeraccount, Integer.valueOf(opeorgcookie), customertype)
					.size() == 0) {
				customeraccount = sdf.format(new Date()) + "0001";
			} else {
				String s = customerdao
						.findByCustomeraccountLikeDESC(customeraccount, Integer.valueOf(opeorgcookie), customertype)
						.get(0).getCustomeraccount();
				s = s.substring(s.length() - 4);
				Integer i = Integer.valueOf(s) + 1;
				s = String.format("%04d", i);
				customeraccount = sdf.format(new Date()) + s;
			}
			customer.setCustomeraccount(customeraccount);
			customer.setCustomername(customername);
			customer.setCustomeraddress(customeraddress);
			customer.setCustomertype(customertype);
			customer.setCustomerorg(Integer.valueOf(opeorgcookie));
			customer.setCustomerstatus("1");
			customer.setCustomerrechargestatus("1");
			customer.setCustomerphone(customerphone);
			customer.setCustomerwechat(customerwechat);
			customerdao.save(customer);

			return new JsonResult<Customer>(91, "增加用户成功", customer);
		}

	}

	// 删除
	@Transactional
	public JsonResult<Customer> deletecustomer(Integer customerid) {

		if (customerid != null) {
			if (customerdao.findOne(customerid).getCustomerstatus().equals("1")) {

				customerdao.delete(customerid);

				return new JsonResult<Customer>(94, "删除用户成功", null);
			}
			return new JsonResult<Customer>(101, "删除用户失败..该用户正在与表连接", null);
		}
		return new JsonResult<Customer>(100, "删除用户失败..ID为空", null);
	}

	// 修改页面
	@Transactional
	public JsonResult<FileCustomer> updateviewcustomer(Integer customerid, String languagecookie) {

		if (customerid != null) {

			Customer customer = customerdao.findOne(customerid);
			log.info(customerid.toString());

			return new JsonResult<FileCustomer>(95, "查单个用户", dofilecustomer(customer, languagecookie));
		}
		return new JsonResult<FileCustomer>(99, "查单个用户失败..ID为空", null);
	}

	// 修改
	@Transactional
	public JsonResult<Customer> updatecustomer(Integer customerid, String customername, String customeraddress,
			String customertype, String opeorgcookie, String customerstatus, String customerphone,
			String customerwechat) {

		Customer customer = new Customer();

		if (customerid == null || Utils.isEmpty(customername) || Utils.isEmpty(customeraddress)
				|| Utils.isEmpty(customertype) || Utils.isNull(opeorgcookie) || Utils.isEmpty(customerstatus)
				|| Utils.isEmpty(customerphone) || Utils.isEmpty(customerwechat)) {

			return new JsonResult<Customer>(97, "修改用户失败..数值不能为空", null);
		} else {

			customer.setCustomerid(customerid);
			customer.setCustomerorg(customerdao.findOne(customerid).getCustomerorg());
			customer.setCustomeraccount(customerdao.findOne(customerid).getCustomeraccount());
			customer.setCustomerrechargestatus(customerdao.findOne(customerid).getCustomerrechargestatus());
			customer.setCustomerstatus(customerdao.findOne(customerid).getCustomerstatus());

			// 判断customername
			if (Utils.isNull(customername)) {
				customer.setCustomername(customerdao.findOne(customerid).getCustomername());

			} else {
				customer.setCustomername(customername);
			}
			// 判断customeraddress
			if (Utils.isNull(customeraddress)) {
				customer.setCustomeraddress(customerdao.findOne(customerid).getCustomeraddress());

			} else {
				customer.setCustomeraddress(customeraddress);
			}
			// 判断customertype
			if (Utils.isNull(customertype)) {
				customer.setCustomertype(customerdao.findOne(customerid).getCustomertype());

			} else {
				customer.setCustomertype(customertype);
			}
			// 判断customerphone
			if (Utils.isNull(customerphone)) {
				customer.setCustomerphone(customerdao.findOne(customerid).getCustomerphone());

			} else {
				customer.setCustomerphone(customerphone);
			}
			// 判断customerwechat
			if (Utils.isNull(customerwechat)) {
				customer.setCustomerwechat(customerdao.findOne(customerid).getCustomerwechat());

			} else {
				customer.setCustomerwechat(customerwechat);
			}

			customerdao.save(customer);
			log.info("UpdateSuccess");
			return new JsonResult<Customer>(96, "修改用户成功", customer);
		}

	}

	// 组装前端类
	public FileCustomer dofilecustomer(Customer customer, String languagecookie) {

		FileCustomer filecustomer = new FileCustomer();

		filecustomer.setCustomerid(customer.getCustomerid());
		filecustomer.setCustomeraccount(customer.getCustomeraccount());
		filecustomer.setCustomername(customer.getCustomername());
		filecustomer.setCustomeraddress(customer.getCustomeraddress());
		filecustomer.setCustomertype(customer.getCustomertype());
		filecustomer.setCustomertypename(sysdicservice.finddisplay("0006", customer.getCustomertype(), languagecookie));
		filecustomer.setCustomerorg(customer.getCustomerorg());
		filecustomer.setCustomerorgname(
				sysdicservice.finddisplay("0001", customer.getCustomerorg().toString(), languagecookie));
		filecustomer.setCustomerstatus(customer.getCustomerstatus());

		// 如果已连接表，查到连接的哪个表
		if (customer.getCustomerstatus().equals("2")) {
			Integer meterid = customermeterdao.findByCustomerid2(customer.getCustomerid()).getPk().getMeterid();
			String meterassetcode = meterdao.findOne(meterid).getMeterassetcode();
			String metertype = meterdao.findOne(meterid).getMeterkeytype();
			filecustomer.setCustomerstatusname(meterassetcode);
			filecustomer.setMeterid(meterid);
			filecustomer.setMeterkeytype(sysdicservice.finddisplay("0031", metertype, languagecookie));
			if (childmeterdao.findByMeterid(meterid) == null) {
				filecustomer.setChildrenmeterstatus("未设置");

			} else {
				String childmetertype = childmeterdao.findByMeterid(meterid).getType();
				switch (childmetertype) {
				case "1":
					filecustomer.setChildrenmeterstatus("总");
					break;
				case "2":
					filecustomer.setChildrenmeterstatus("分");
					break;
				default:
					break;
				}
			}

		} else {
			filecustomer.setCustomerstatusname(
					sysdicservice.finddisplay("0018", customer.getCustomerstatus(), languagecookie));
			filecustomer.setChildrenmeterstatus("未设置");
		}

		filecustomer.setCustomerphone(customer.getCustomerphone());
		filecustomer.setCustomerwechat(customer.getCustomerwechat());
		String rechargestatus = customer.getCustomerrechargestatus();
		if (languagecookie.equals("cn")) {
			switch (Integer.valueOf(rechargestatus)) {
			case 2:
				filecustomer.setCustomerrechargestatus("正常");
				break;
			case 3:
				filecustomer.setCustomerrechargestatus("待销户");
				break;
			case 4:
				filecustomer.setCustomerrechargestatus("已销户");
				break;
			}
		} else if (languagecookie.equals("en")) {
			switch (Integer.valueOf(rechargestatus)) {
			case 2:
				filecustomer.setCustomerrechargestatus("正常en");
				break;
			case 3:
				filecustomer.setCustomerrechargestatus("待销户en");
				break;
			case 4:
				filecustomer.setCustomerrechargestatus("已销户en");
				break;
			}

		}
		filecustomer.setCustomerrechargestatus(customer.getCustomerrechargestatus());

		return filecustomer;

	}

	// 根据用户ID查询所连表的详细信息
	@Transactional
	public JsonResult<FileMeter> findMeterBycustomerID(Integer customerid, String languagecookie) {
		Integer meterid = customermeterdao.findByCustomerid2(customerid).getPk().getMeterid();
		Meter meter = meterdao.findOne(meterid);

		return new JsonResult<FileMeter>(102, "根据用户ID查询所连表的详细信息", meterservice.dofilemeter(meter, languagecookie));
	}

	// 新增时自动展示用户类型
	@Transactional
	public JsonResult<String> listcustomertype(String languagecookie, String typecookie) {
		String customertype = sysdicservice.finddisplay("0006", typecookie, languagecookie);
		return new JsonResult<String>(215, "Customertype字段列表", customertype);
	}

}
